/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn;

/**
 * <p>Project: app2022 - Ex4
 * <p>Powered by webrx On 2022-01-14 09:04:53
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex4 {

    public static void main(String[] args) {
        String t = "java";
        String str = "javajavahtmljavajavaspjavaservletjava-java";
        int index = 0;
        int num = 0;
        while(str.indexOf(t,index)!=-1){
            ++num;
            index=str.indexOf(t,index)+t.length();
        }
        System.out.println(num);
    }

    //判断字符串出现的次数
    public static void ex(String[] args) {
        String str = "java";
        String teststr = "javajavahtmljspjavaservletjava";
        //判断字符串有几位
        //从第一个字符（检索位置+字符串长度）截取出来
        //跟规定字符串比较
        //相同次数加一不相同次数不变继续循环

        int strnum = str.length();
        int num = 0;
        for (int i = 0; i < teststr.length(); i++) {
            //老师我对这个判断中的teststr.length()+1理解不是很透，知道这个地方要+1就是具体的理由说不上来
            if (i + strnum < teststr.length() + 1) {
                String checkstr = teststr.substring(i, i + strnum);
                if (str.equals(checkstr)) {
                    ++num;
                }
            }
        }
        System.out.println(num);
    }
}
